/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.new9uy.tetris;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.new9uy.tetris.util.SwingUtils;

/**
 *
 * @author Overmind
 */
public class GameData
{
    public static void save(String name, int[] controls)
    {
        try (FileWriter sortie = new FileWriter("save.txt"))
        {
            sortie.write(name + "\n");
            for (int i = 0; i < 5; i++)
            {
                sortie.write(String.valueOf(controls[i]) + "\n");
            }
            sortie.flush();
        } 
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
    
    public static void load() throws FileNotFoundException
    {
        File f = new File("save.txt");

        try (BufferedReader entree = new BufferedReader(new FileReader(f)))
        {
            Settings.PLAYER_NAME = entree.readLine();
            Settings.CONTROLLER_DROP = Integer.parseInt(entree.readLine());
            Settings.CONTROLLER_LEFT = Integer.parseInt(entree.readLine());
            Settings.CONTROLLER_RIGHT = Integer.parseInt(entree.readLine());
            Settings.CONTROLLER_SPEEDUP = Integer.parseInt(entree.readLine());
            Settings.CONTROLLER_ROTATE = Integer.parseInt(entree.readLine());

        } 
        catch (IOException ioe)
        {
            System.out.println(ioe.getMessage());

            SwingUtils.infoMessage(null,
                    "The save file appears to be corrupted.\n"
                    + "The game will reset.",
                    "Corruption");
            f.delete();
            throw new FileNotFoundException("corruption");
        }
    }
}
